<?php

namespace App\Http\Controllers\Strator;

use App\Http\Controllers\Controller;
use App\Http\Requests\Strator\StoreStratorProRequest;
use Illuminate\Database\Eloquent\Model;
use App\Models\Strator\StratorPro;
use App\Models\Strator\StratorProduct;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class StratorProController extends Controller
{
    protected Request $request;
    protected Model $model;

    public function __construct(Request $request,StratorPro $strator)
    {
        $this->request=$request;
        $this->model = $strator;
    }

    /**
     * Display a listing of the resource.
     */
    public function inventory()
    {
        $perPage=$this->request->per_page??10;
        $list=$this->model
            ->modelSearch($this->request,$this->model)
            ->where('inventory','>',0)
            ->with(['type','sku'])
            ->orderByDesc('id')
            ->paginate($perPage);
        $this->saveOperationLog('查询库存列表');
        return $this->success($list);
    }

    /**
     * Display a listing of the resource.
     */
    public function warning()
    {
        $perPage=$this->request->per_page??10;
        $list=$this->model
            ->modelSearch($this->request,$this->model)
            ->where('inventory','>',0)
            ->whereColumn('warning', '>=', 'inventory')
            ->with(['type','sku'])
            ->orderByDesc('id')
            ->paginate($perPage);
        $this->saveOperationLog('查询预警列表');
        return $this->success($list);
    }

    protected function searchOrderModel($request, $model){
        if($request->filled('search')){
            $search = $request->search;
            $model = $model->where(function($query)use($search){
                $query->where('name','like','%'.$search.'%')
                    ->orWhere('itemno','like','%'.$search.'%');
            });
        }
        return $model;
    }

    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $perPage=$this->request->per_page??10;
        $model = $this->searchOrderModel($this->request,$this->model);
        if($this->request->filled('type_id')){
            $model = $model->where('type_id', $this->request->type_id);
        }
        $list=$model->with(['type','sku.attr'])
        ->orderByDesc('id')
        ->paginate($perPage);
        $this->saveOperationLog('查询主产品列表');
        return $this->success($list);
    }
    public function listall()
    {
        $list=$this->model->get();
        return $this->success($list);
    }

    /**
     * Show the form for creating a new resource.干什么？
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(StoreStratorProRequest $request)
    {
        $data=$this->request->only($this->storeOnly());
        $this->model->fill($data);
        $this->model->save();
        $this->saveOperationLog('添加主产品');
        // $this->buildDetail($this->request,$this->model->id);
        return $this->success(['id'=>$this->model->id]);
    }
    protected function buildDetail($request,$pro_id){
        // 按数据生成出入库明细
        $contents = json_decode($request->contents);
        $data = [];
        for ($i=0;$i<count($contents);$i++){
            if(!empty($contents[$i]->id)){
                // 有id是更新
                StratorProduct::where('id', $contents[$i]->id)
                ->update([
                    'updated_at'=>now(),
                    'sort'=>$contents[$i]->sort??0,
                    'diy_attr'=>$contents[$i]->diy_attr??null,
                    'remark'=>$request->remark??'',
                    'spec_contents'=>$request->spec_contents??'',
                    'type_id'=>$contents[$i]->type_id??0,
                    'type_name'=>$contents[$i]->type_name??null,
                    'type_name_fill'=>$contents[$i]->type_name_fill??null,
                    'name'=>$contents[$i]->name??'',
                    'attr_id'=>$contents[$i]->attr_id??0,
                    'attr_name'=>$contents[$i]->attr_name??null,
                    'attr_name_fill'=>$contents[$i]->attr_name_fill??null,
                    'images'=>$contents[$i]->images??null,
                    'cost_price'=>$request->cost_price??'0.00',
                    'make_price'=>$request->make_price??'0.00',
                    'sale_price'=>$request->sale_price??'0.00',
                    'wholesale_price'=>$request->wholesale_price??'0.00',
                ]);
            }else{
                // 无id是新增
                $data[$i]= [
                    'pro_id'=> $pro_id,
                    'created_at'=>now(),
                    'updated_at'=>now(),
                    'sort'=>$contents[$i]->sort??0,
                    'diy_attr'=>$contents[$i]->diy_attr??null,
                    'remark'=>$request->remark??'',
                    'spec_contents'=>$request->spec_contents??'',
                    'type_id'=>$contents[$i]->type_id??0,
                    'type_name'=>$contents[$i]->type_name??null,
                    'type_name_fill'=>$contents[$i]->type_name_fill??null,
                    'name'=>$contents[$i]->name??'',
                    'attr_id'=>$contents[$i]->attr_id??0,
                    'attr_name'=>$contents[$i]->attr_name??null,
                    'attr_name_fill'=>$contents[$i]->attr_name_fill??null,
                    'images'=>$contents[$i]->images??null,
                    'cost_price'=>$request->cost_price??'0.00',
                    'make_price'=>$request->make_price??'0.00',
                    'sale_price'=>$request->sale_price??'0.00',
                    'wholesale_price'=>$request->wholesale_price??'0.00',
                ];
            }
        }

        if(count($data)>0){
            DB::table('strator_products')->insert($data);
        }
    }
    //
    public function storeOnly():array{
        return [
            'type_id',
            'type_name',
            'type_name_fill',
            'sort',
            'status',
            'name',
            'images',
            'remark',
            'contents',
            'operator',
            'operator_id',
            'make_price',
            'cost_price',
            'sale_price',
            'wholesale_price',
            'spec_contents',
            'itemno',
        ];
    }

    /**
     * Display the specified resource.
     */
    public function show()
    {
//        return $this->success($this->request->user());
        $model=$this->model->with(['type','sku.attr'])->findOrFail($this->request->id);
        return $this->success($model);
    }

    /**
     * Update the specified resource in storage.
     */
    public function update()
    {
        $model=$this->model->findOrFail($this->request->id);
        $origin=$model->toArray();
        $data=$this->request->except($this->updateExcept());
        $model->fill($data);
        $model->save();
        $this->saveOperationLog('修改更新主产品',$origin);
        // $this->buildDetail($this->request,$this->request->id);
        return $this->successMsg();
    }
    //
    public function updateExcept():array
    {
        return [
            'id',
        ];
    }
    /**
     * Remove the specified resource from storage.
     */
    public function destroy()
    {
        $model=$this->model->findOrFail($this->request->id);
        $model->delete();
        // 同步删除sku
        StratorProduct::where('pro_id', $this->request->id)->update(['deleted_at'=>now()]);
        $this->saveOperationLog('删除主产品');
        return $this->successMsg('删除成功');
    }
}
